﻿#include "virscan.h"
#include "dynamicbtn.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <QTimer>
#include <QCheckBox>
#include <QPushButton>

#include "rrogress.h"
#include "utils.h"


QWidget *CreateTop(QString icon, QString title, QString tip)
{
    QWidget *item = new QWidget();
    QLabel *labelIcon = new QLabel();
    QLabel *labelTitle = CreateCustomLabel(title, "#444444", 20);
    QLabel *labelTip = CreateCustomLabel(tip, "#777777", 12);

    labelIcon->setPixmap(QPixmap(icon));
    labelIcon->setFixedWidth(60);

    QHBoxLayout *layoutTop = new QHBoxLayout();
    layoutTop->addWidget(labelTitle);
    layoutTop->addStretch(1);
    layoutTop->addWidget(CreateCustomButton("停止"));
    layoutTop->addWidget(CreateDefaultButton("暂停"));
    layoutTop->setSpacing(20);

    QVBoxLayout *layoutRight = new QVBoxLayout();
    layoutRight->addStretch(1);
    layoutRight->addLayout(layoutTop);
    layoutRight->addWidget(labelTip);
    layoutRight->addStretch(1);
    layoutRight->setSpacing(2);
    layoutRight->setContentsMargins(10, 0, 0, 0);

    QHBoxLayout *layoutMain = new QHBoxLayout(item);
    layoutMain->addWidget(labelIcon);
    layoutMain->addLayout(layoutRight);
    layoutMain->setContentsMargins(0, 0, 0, 0);
    layoutMain->setSpacing(10);

    return item;
}

Virscan::Virscan(QWidget *parent) : BaseChildPane(parent)
{
    m_nStep = 1;
    m_strTitle = "病毒查杀";
    m_pTimer = new QTimer();
    InitCtrl();

    connect(m_pTimer, &QTimer::timeout, [=]() {
        m_nStep++;

        QList<DynamicBtn *> listChilds = this->findChildren<DynamicBtn *>();
        foreach (DynamicBtn *var, listChilds) {
            var->Stop();
        }

        int nIndex = qMin(m_nStep - 1, listChilds.length() - 1);
        listChilds.at(nIndex)->Start();

        if (m_nStep == listChilds.length())
        {
           m_pTimer->stop();
        }
    });

    m_pTimer->start(10 * 1000);
}

void Virscan::InitCtrl()
{
    QGridLayout *layoutMain = new QGridLayout();
    DynamicBtn *btn1 = new DynamicBtn();
    btn1->SetParam(":/img/100/viruscan/boot_sector.png", "引导区", "等待扫描");
    layoutMain->addWidget(btn1, 0, 0, 1, 1);
    DynamicBtn *btn2 = new DynamicBtn();
    btn2->SetParam(":/img/100/viruscan/sys_program.png", "系统进程", "等待扫描");
    layoutMain->addWidget(btn2, 0, 1, 1, 1);
    DynamicBtn *btn3 = new DynamicBtn();
    btn3->SetParam(":/img/100/viruscan/startup.png", "启动项", "等待扫描");
    layoutMain->addWidget(btn3, 0, 2, 1, 1);
    DynamicBtn *btn4 = new DynamicBtn();
    btn4->SetParam(":/img/100/viruscan/sevices_drives.png", "服务与驱动", "等待扫描");
    layoutMain->addWidget(btn4, 1, 0, 1, 1);
    DynamicBtn *btn5 = new DynamicBtn();
    btn5->SetParam(":/img/100/viruscan/browser_plugin.png", "系统组件", "等待扫描");
    layoutMain->addWidget(btn5, 1, 1, 1, 1);
    DynamicBtn *btn6 = new DynamicBtn();
    btn6->SetParam(":/img/100/viruscan/sys_position.png", "系统关键位置", "等待扫描");
    layoutMain->addWidget(btn6, 1, 2, 1, 1);
    DynamicBtn *btn7 = new DynamicBtn();
    btn7->SetParam(":/img/100/viruscan/local_disk.png", "本地磁盘", "等待扫描");
    layoutMain->addWidget(btn7, 0, 3, 2, 1);
    layoutMain->setSpacing(10);


    QVBoxLayout *layoutMain1 = new QVBoxLayout(this);

    layoutMain1->addWidget(CreateTop(":/img/100/viruscan/full_title.png", "正在进行全盘扫描", "disk:/A\.\\PhysicalDrive1"));

    Progress *pProgress = new Progress();
    pProgress->setFixedHeight(3);

    QHBoxLayout *layoutCfg = new QHBoxLayout();
    QButtonGroup *group;
    layoutCfg->addWidget(CreateButtonGroup(QStringList() << "常规" << "高速", &group));
    connect(group, static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked), [=](int id) {
       qDebug() << id;
    });
    layoutCfg->addWidget(CreateCheckBox("启用GPU加速"));
    layoutCfg->addWidget(CreateCheckBox("查杀完成后自动关机"));
    layoutCfg->addStretch(1);
    layoutCfg->addWidget(CreateCustomLabel("已用时间：00:00:05", "#777777", 12));

    layoutMain1->addWidget(pProgress);
    layoutMain1->addLayout(layoutCfg);
    layoutMain1->addLayout(layoutMain, 1);
    layoutMain1->setContentsMargins(40, 20, 40, 20);
    layoutMain1->setSpacing(10);
}

void Virscan::Start()
{
    m_nStep = 1;

    QList<DynamicBtn *> listChilds = this->findChildren<DynamicBtn *>();
    foreach (DynamicBtn *var, listChilds) {
        var->Stop();
    }

    listChilds.at(0)->Start();
    m_pTimer->start(10 * 1000);
}

void Virscan::Stop()
{
    m_pTimer->stop();

    QList<DynamicBtn *> listChilds = this->findChildren<DynamicBtn *>();
    foreach (DynamicBtn *var, listChilds) {
        var->Clear();
    }
}

void Virscan::Pause()
{

}

void Virscan::OpenPage()
{
    Start();
}

void Virscan::ClosePage()
{
    Stop();
}
